package com.zh.util;

import cn.hutool.extra.mail.Mail;
import com.zh.sys.service.EmployeeService;
import com.zhang.entity.Employee;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: 96199
 * @className: Mytask
 * @description:
 * @date: 2021/6/24 15:01
 * @version: 0.1
 * @since: jdk1.8
 */
@Component
public class Mytask {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private RedisTemplate<String,Long> redisTemplate;

    @Async
    public void sendEmail(List<Employee> employees,String email){
        List<String> emails = new ArrayList<>();
        for (Employee employee : employees) {
            emails.add(employee.getEmall());
        }
        try {
            MyMail.mailLogin("<h1>请部门主管及时为员工分配导师<h1>",email,emails);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Async
//    @Scheduled(cron = "0/20 * * * * ?")
    @Scheduled(cron = "0 0 9 ? * FRI")
    public void sendEmail2(){
        Employee employee = new Employee();
        employee.setIsOfficial(0);
        employee.setIsDel(0);
        employee.setIsUpdate(1);
        List<Employee> employees = employeeService.queryAll(employee);
        ValueOperations<String, Long> opsForValue = redisTemplate.opsForValue();
        TreeSet<String> set = new TreeSet<>();
        for (Employee employee1 : employees) {
            Long s = opsForValue.get(employee1.getName()+":"+employee1.getEmall());
            System.out.println(s);
            assert s != null;
            Long day = MyUtil.getDay(s);
            if (day<=7){
                set.add(employee1.getDepartId());
            }
        }
        List<String> strings = new ArrayList<>(set);
        List<Employee> employees1 = employeeService.queryTid(strings);
        List<String> list = new ArrayList<>();
        for (Employee employee1 : employees1) {
           list.add(employee1.getEmall());
        }
        try {
            MyMail.mailLogin("<h1>请主管及时为员工指定计划</h1>","961995531@qq.com",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Async
    @Scheduled(cron = "0 15 10 ? * *")
    public void sendEmail3(){
        Employee employee = new Employee();
        employee.setIsOfficial(0);
        employee.setIsDel(0);
        employee.setIsUpdate(1);
        List<Employee> employees = employeeService.queryAll(employee);
        ValueOperations<String, Long> opsForValue = redisTemplate.opsForValue();
        List<String> list =new ArrayList<>();
        for (Employee employee1 : employees) {
            Long s = opsForValue.get(employee1.getName()+":"+employee1.getEmall());
            assert s != null;
            Long day = MyUtil.getDay(s);
            if (day==30||day==60){
                Employee employee2 = employeeService.queryById(employee1.getTid());
                try {
                    MyMail.mailLogin("<h1>请导师及时为员工辅导情况进行总结</h1>",employee2.getEmall(),list);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
